package com.isotronica.neveras.presentacion;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.ScrollPane;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.SwingWorker;

public class VirtualConsole extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JTextArea consolTextArea;
	
	private JLabel titulo;
	private JScrollPane scroll;
	private static final Dimension screen = Toolkit.getDefaultToolkit()
	.getScreenSize();
	

	public VirtualConsole() {
			
		initComponents();
		
	}

	private void initComponents() {	
		
		titulo = new JLabel("Virtual Console, for G-Thux server V4.0");
		scroll = new JScrollPane();
		consolTextArea = new JTextArea();
		
		scroll.setViewportView(consolTextArea);

		// ======== this ========
		setBackground(Color.white);
		setForeground(Color.white);
		setTitle("Virtual Console --- G-Thux ---");
		setLayout(new BorderLayout());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setPreferredSize(new Dimension(500,500));

		
		// ======== Panel general ========
		{
			add(scroll, BorderLayout.CENTER);
			add(titulo, BorderLayout.NORTH);
			
		}
		pack();
		setLocation((screen.width - this.getSize().width / 2) / 2,
				(screen.height - this.getSize().height / 2)/2);
		setVisible(true);
		
	}

	public void setMensaje(final String mensaje) {
		
		/*
		final Dialogo dialogo = new Dialogo();
		dialogo.setVisible(true);
		dialogo.pack();
		 */
		final SwingWorker<Boolean, Boolean> worker = new SwingWorker<Boolean, Boolean>() {
			@Override
			protected Boolean doInBackground() throws Exception {
				consolTextArea.setText(mensaje);
				consolTextArea.setCaretPosition(consolTextArea.getDocument().getLength());
				//consolTextArea.scrollRectToVisible(
					//	  new Rectangle(0,consolTextArea.getHeight()-2,1,1));


				return true;
			}
			
			@Override
			public void done() {
				validate();				
				//dialogo.dispose();
				System.gc();
			}
		};
		worker.execute();
	}

	
}

class Dialogo extends JDialog {
	private static final Dimension screen = Toolkit.getDefaultToolkit()
			.getScreenSize();
	private static final long serialVersionUID = 1L;
	JProgressBar progressBar;

	public Dialogo() {
		setLocation((screen.width - this.getSize().width / 2) / 2,
				screen.height / 2);
		setLayout(new FlowLayout());
		setUndecorated(true);
		setAlwaysOnTop(true);
		progressBar = new JProgressBar();
		progressBar.setIndeterminate(true);
		progressBar.setBorder(BorderFactory.createTitledBorder(BorderFactory
				.createEmptyBorder()));
		add(progressBar);
	}
}


